/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.upc.dew.cobranzas.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import pe.edu.upc.dew.cobranzas.model.Contrato;
import pe.edu.upc.dew.cobranzas.model.Cuota;
import pe.edu.upc.dew.cobranzas.service.ContratoServiceImpl;

/**
 *
 * @author Ivan
 */
public class CuotaDaoImpl implements CuotaDao {

    private ContratoServiceImpl contratoService;

    public void setContratoService(ContratoServiceImpl contratoService) {
        this.contratoService = contratoService;
    }
    public List<Cuota> cuotas = new ArrayList(); //ver

    public boolean Registra(int idCuota, String fecha, String estado, int idContrato) {

        Connection connection = null;
        Statement st = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            st.executeQuery("insert into T_Cuota values (" + idCuota + ",'" + fecha + "','" + estado + "'," + idContrato + ")");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {

            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
            return true;
        }
    }

    public boolean Elimina(Contrato contrato) {

        Connection connection = null;
        Statement st = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            st.executeQuery("delete from t_cuota where idContrato=" + contrato.getIdContrato() + "");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {

            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
            return true;
        }

    }

    public List<Cuota> Listado(int idContrato) {
        cuotas.clear();
        Connection connection = null;
        Cuota cuota = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            rs = st.executeQuery("select * from T_Cuota where idContrato=" + idContrato + " order by idCuota");
            rs.next();
            do {

                cuota = new Cuota();
                cuota.setIdCuota(rs.getInt("idCuota"));
                cuota.setFecha(rs.getString("fecha"));
                cuota.setEstado(rs.getString("estado"));
                cuota.setContrato(contratoService.getContratoPorCodigo(idContrato));
                this.cuotas.add(cuota);

            } while (rs.next());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
        return cuotas;
    }

    public Cuota getCuota(Contrato contrato, int idCuota) {
        this.Listado(contrato.getIdContrato());
        Cuota cuotaEncontrada = null;
        for (int i = 1; i <= this.cuotas.size(); i++) {

            if (this.cuotas.get(i - 1).getContrato().getIdContrato() == contrato.getIdContrato() && this.cuotas.get(i - 1).getIdCuota() == idCuota) {
                cuotaEncontrada = this.cuotas.get(i - 1);
            }
        }
        return cuotaEncontrada;
    }

    public int getCuotaPendiente(int idContrato) {
        int idCuota = 0;
        Connection connection = null;
        ResultSet rs = null;
        Statement st = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            rs = st.executeQuery("select min(idCuota) as cuota from t_cuota where idContrato=" + idContrato + " and estado='Pendiente'");
            if (rs.next()) {
                idCuota = rs.getInt("cuota");

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);

        }
        return idCuota;
    }

    public boolean actualizarEstado(Contrato contrato, Cuota cuota, String estado) {
        Connection connection = null;
        Statement st = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            st.executeQuery("update t_cuota set estado='" + estado + "' where idContrato=" + contrato.getIdContrato() + " and idCuota=" + cuota.getIdCuota() + "");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {

            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
            return true;
        }

    }
}
